/*
 * Copyright (c) 2013-2015 SimaStudio. All rights reserved.
 *
 * This software is the confidential and proprietary information of SimaStudio.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with SimaStudio.
 *
 * SimaStudio MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * SimaStudio SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.asiainfo.integration.xml;

import com.asiainfo.integration.utils.ConfigProvider;
import org.codehaus.stax2.XMLInputFactory2;
import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.evt.XMLEvent2;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Usage:
 *
 * @author Charkey
 * @date 4/24/2016
 */
@SuppressWarnings("Since15")
public class XmlXPathTester {

    public static void main(String[] args) {
        try {
            URL url = ConfigProvider.class.getClassLoader().getResource("config/task/createServiceOrder-1000042367-NO000004MCNA.xml");
            InputStream in = new FileInputStream(new File(url.toURI().getPath()));
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = builder.parse(in);
            XPath xpath = XPathFactory.newInstance().newXPath();
            String expression = "/CreateServiceOrderRequest/orderHeader/orderNumber";
            Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
            System.out.println(widgetNode.getNodeName());//节点名称 ns0:orderNumber
            System.out.println(widgetNode.getNodeValue());//节点值是null，为什么？ null
            System.out.println(widgetNode.getTextContent());//节点text 1000042367

            URL url2 = ConfigProvider.class.getClassLoader().getResource("config/task/createServiceOrder-1000042367-NO000004MCNA.xml");
            InputStream in2 = new FileInputStream(new File(url2.toURI().getPath()));
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            //domFactory.setNamespaceAware(true); // never forget this!
            //domFactory.setNamespaceAware(false); // never forget this!
            DocumentBuilder builder2 = domFactory.newDocumentBuilder();
            Document document2 = builder2.parse(in2);
            XPath xpath2 = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xpath2.compile("//*[local-name()='orderNumber']");
            Object result = expr.evaluate(document2, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getTextContent());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }
}